package util;

import freemarker.template.utility.DateUtil;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by eleven on 15/12/30.
 */
public class StringUtil {
    public static boolean isEmpty(String str) {
        return str == null || str.trim().length() == 0;
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static Date strToDate(String str,String format){
        DateFormat dateFormatformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        if (StringUtil.isNotEmpty(str)) {
        if (format.equals("yyyy-MM-dd"))
         dateFormatformat = new SimpleDateFormat("yyyy-MM-dd");
        if (format.equals("yyyyMMdd"))
            dateFormatformat = new SimpleDateFormat("yyyyMMdd");
        if (format.equals("yyyy年"))
            dateFormatformat = new SimpleDateFormat("yyyy年");
            if (format.equals("yyyyF")) {
                dateFormatformat = new SimpleDateFormat("yyyy");
                str=str.substring(0,4);
                System.out.print(str);
            }
        if (format.equals("yyyy.MM"))
            dateFormatformat = new SimpleDateFormat("yyyy.MM");
        if (format.equals("ytwo"))
            dateFormatformat = new SimpleDateFormat("yyyy年度");
        if (format.equals("dd/MM/yyyy"))
            dateFormatformat = new SimpleDateFormat("dd/MM/yyyy");
        if (format.equals("yyyyMMddHHmmss"))
            dateFormatformat = new SimpleDateFormat("yyyyMMddHHmmss");
        if (format.equals("weeks")) {
            int p= str.indexOf("星");
             str = str.substring(0,p-1);
            dateFormatformat = new SimpleDateFormat("yyyy-MM-dd");
        }
        if (format.equals("toup")) {
            int p= str.indexOf("至");
            str = str.substring(0,p);
            dateFormatformat = new SimpleDateFormat("yyyy-MM-dd");
        }
        if (format.equals("todown")) {
            int p= str.indexOf("至");
            str = str.substring(p+1,str.length());
            dateFormatformat = new SimpleDateFormat("yyyy-MM-dd");
        }
            try {
                // Fri Feb 24 00:00:00 CST 2012
                date = dateFormatformat.parse(str);
//            System.out.println(str);
//            System.out.println(date);
            } catch (ParseException e) {
                System.out.println("strToDate error");
                e.printStackTrace();
            }
        }
        // 2012-02-24
//        date.setTime(Long.valueOf(str));
//        date = java.sql.Date.valueOf(str);
//        Date t =java.sql.Date.valueOf("yyyy-MM-dd") ;
        return date;
    }

    public static Date strToDateTrans(String str){
        DateFormat dateFormatformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            if (str.contains(".")) {
                dateFormatformat = new SimpleDateFormat("yyyy.MM");
                date = dateFormatformat.parse(str);
            } else if (str.contains("-") && str.contains(":")) {
                date = dateFormatformat.parse(str);
            }
        }catch (Exception e){
            System.out.println("strToDate error");
            e.printStackTrace();
        }
        return date;
    }
    public static Date yearAdd(String str1 ,String str2){
        DateFormat dateFormatformat = new SimpleDateFormat("yyyy");
        Date date = null;
        if (StringUtil.isNotEmpty(str1)&&str1.substring(0,1)=="2") {
            try {
                // Fri Feb 24 00:00:00 CST 2012
                date = dateFormatformat.parse(str1.substring(0,4));
//            System.out.println(str);
//            System.out.println(date);
            } catch (ParseException e) {
                System.out.println("yearAdd error");
                e.printStackTrace();
            }


        str2=str2.substring(0,1);
//System.out.print(str2);
        return DateUtils.addYears(date,Integer.parseInt(str2));
        }else
        {
            return null;
        }

    }

}
